package com.hmall.item.web;

import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Delete;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    /**
     * 分页查询商品
     */
    @GetMapping("/list")
    public PageDTO<Item> list(Integer page, Integer size){
        return itemService.list(page,size);
    }

    //请求 URL: http://localhost:10010/item/7410809
    // 请求方法: GET
    /**
     * 根据id查询商品
     */
    @GetMapping("/{id}")
    public Item findById(@PathVariable Long id){//路径变量
    return itemService.findById(id);
    }

    //新增商品
    @PostMapping
    public void addItem(@RequestBody Item item){
    itemService.addItem(item);
    }

    //商品上架(1)，下架(2)
    @PutMapping("/status/{id}/{status}")
    public void switStatu(@PathVariable Long id,@PathVariable Integer status){
        itemService.switStatu(id,status);
    }

    //修改商品
    @PutMapping
    public void updateItem(@RequestBody Item item){
        itemService.updateItem(item);
    }

    //根据id删除商品
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
        itemService.deleteById(id);
    }

    //获取记录总条数
    @GetMapping
    public Integer getTotal(){
       return itemService.getTotal();
    }

    //扣减库存接口
    @PutMapping("/stock/{itemId}/{num}")
    public void subStockByItemId(@PathVariable Long itemId,@PathVariable Integer num){
        itemService.subStockByItemId(itemId,num);
    }
}
